﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Drawing;
using System.Data;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using MySql.Data.MySqlClient;
using MySql.Data.Types;

namespace Gui.Functies
{
    public partial class Agendapunten : UserControl
    {
        String date = "";
        String note = "";
        Boolean error = false;
        int projectnummer;

        public Agendapunten(int projectID)
        {
            InitializeComponent();
            projectnummer = projectID;
            button1.Hide();
            richTextBox1.Hide();
            label2.Hide();
        }

        private void button1_Click(object sender, EventArgs e)
        {
            date = textBox1.Text;
            note = richTextBox1.Text;

            if (date != "" && note != "")
            {
                saveNote();
                this.Dispose(false);
            }
            else
            {
                MessageBox.Show("Niet alle velden zijn ingevuld!");
            }
        }

        public void saveNote()
        {
            MySqlConnection grabConnection = new MySqlConnection("Server=qq-design.com;Port=3306;Database=cookicom0;Uid=cookicom0;pwd=pizzahut91");
            MySqlCommand agenda = new MySqlCommand("INSERT INTO agenda (date, note, projectID)" + "VALUES (@date, @note, @projectID)", grabConnection);
            agenda.Parameters.Add(new MySqlParameter("date", date));
            agenda.Parameters.Add(new MySqlParameter("note", note));
            agenda.Parameters.Add(new MySqlParameter("projectID", projectnummer));

            try
            {
                grabConnection.Open();
                agenda.ExecuteNonQuery();

            }
            catch (MySqlException)
            {
                error = true;
            }
            finally
            {
                grabConnection.Close();
                
                if (error == true)
                {
                    MessageBox.Show("Sql error detected!");
                }
                else
                {
                    MessageBox.Show("Agendapunt succesvol opgeslagen!");
                }
            }
        }

        private void monthCalendar1_DateChanged(object sender, DateRangeEventArgs e)
        {
            textBox1.Text = monthCalendar1.SelectionStart.ToString();
        }

        private void button2_Click(object sender, EventArgs e)
        {
            button1.Show();
            button2.Hide();
            richTextBox1.Show();
            label2.Show();
            monthCalendar1.Hide();
        }
    }
}
